// NTWindow.cpp
#include "NTWindow.h"

namespace ANvDbg
{
    HINSTANCE NTWindow::g_app_instance = nullptr;

    NTWindow::NTWindow()
    {
        m_is_show = false;
    }

    LRESULT CALLBACK NTWindow::OpenGLNTWindowProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
    {
        NTWindow* self = (NTWindow*)GetWindowLongPtr(hWnd, GWL_USERDATA);
        if (self == nullptr)
        {
            return DefWindowProc(hWnd, nMsg, wParam, lParam);
        }

        switch (nMsg)
        {
        case WM_CLOSE:
            self->OnClose(wParam, lParam);

            break;
        case WM_LBUTTONDOWN:
            self->OnLButtonDown(wParam, lParam);

            break;
        case WM_LBUTTONUP:
            self->OnLButtonUp(wParam, lParam);

            break;
        case WM_RBUTTONDOWN:
            self->OnRButtonDown(wParam, lParam);

            break;
        case WM_RBUTTONUP:
            self->OnRButtonUp(wParam, lParam);

            break;
        case WM_KEYDOWN:
            self->OnKeyDown(wParam, lParam);

            break;
        case WM_KEYUP:
            self->OnKeyUp(wParam, lParam);

            break;
        default:
            break;
        }

        return DefWindowProc(hWnd, nMsg, wParam, lParam);
    }

    ATOM NTWindow::RegisterNTWindow(LPCTSTR class_name, DWORD style)
    {
        WNDCLASSEX wnd_class;
        memset(&wnd_class, 0, sizeof(WNDCLASSEX));
        wnd_class.cbSize = sizeof(WNDCLASSEX);
        wnd_class.cbClsExtra = 0;
        wnd_class.cbWndExtra = 0;
        wnd_class.hbrBackground = NULL;
        wnd_class.hCursor = LoadCursor(NULL, IDC_ARROW);
        wnd_class.hIcon = NULL;
        wnd_class.hIconSm = NULL;
        wnd_class.hInstance = g_app_instance;
        wnd_class.lpfnWndProc = OpenGLNTWindowProc;
        wnd_class.lpszMenuName = NULL;
        wnd_class.lpszClassName = class_name;
        wnd_class.style = style;

        ATOM atom = RegisterClassEx(&wnd_class);
        return atom;
    }

    void NTWindow::Init()
    {
        m_dc = GetDC(m_hwnd);

        RECT rc;
        GetClientRect(m_hwnd, &rc);
        m_window_width = rc.right - rc.left;
        m_window_height = rc.bottom - rc.top;
    }

    void NTWindow::ShowNTWindow(bool is_show)
    {
        m_is_show = is_show;
        ShowWindow(m_hwnd, m_is_show ? SW_SHOW : SW_HIDE);
        m_is_show && UpdateWindow(m_hwnd);
    }

    void NTWindow::OnClose(WPARAM wParam, LPARAM lParam)
    {
        PostQuitMessage(0);
    }

    void NTWindow::OnLButtonDown(WPARAM wParam, LPARAM lParam)
    {
        printf("OnLButtonDown\n");
    }

    void NTWindow::OnLButtonUp(WPARAM wParam, LPARAM lParam)
    {
        printf("OnLButtonUp\n");
    }

    void NTWindow::OnRButtonDown(WPARAM wParam, LPARAM lParam)
    {
        printf("OnRButtonDown\n");
    }

    void NTWindow::OnRButtonUp(WPARAM wParam, LPARAM lParam)
    {
        printf("OnRButtonUp\n");
    }

    void NTWindow::OnKeyDown(WPARAM wParam, LPARAM lParam)
    {
        printf("OnKeyDown\n");
    }

    void NTWindow::OnKeyUp(WPARAM wParam, LPARAM lParam)
    {
        printf("OnKeyUp\n");
    }
}
